package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.CommentDo;
import com.weijianhuawen.wjblog.common.domain.dos.UserDetailDo;
import com.weijianhuawen.wjblog.common.domain.qo.UserDetailQo;

import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-12-10 14:46
 */
public interface UserDetailMapper extends BaseMapper<UserDetailDo> {
    // 根据邮箱，手机号，qq号查询，仅需查询一个
    default UserDetailDo selectUserByMailOrPhoneOrQQ(String mail, String phone, String qq) {
        return selectOne(Wrappers.<UserDetailDo>lambdaQuery()
                .eq(StringUtils.isNotBlank(mail), UserDetailDo::getMail, mail)
                .or(StringUtils.isNotBlank(phone), wq-> wq.eq(StringUtils.isNotBlank(phone), UserDetailDo::getPhoneNumber, phone))
                .or(StringUtils.isNotBlank(qq), wq -> wq.eq(StringUtils.isNotBlank(qq), UserDetailDo::getQqId, qq))
                .last("LIMIT 1")
        );
    }

    // 通过user_id查询数据
    default UserDetailDo selectUserDetailByUserId(Long userId) {
        return selectOne(Wrappers.<UserDetailDo>lambdaQuery()
                .eq(Objects.nonNull(userId), UserDetailDo::getUserId, userId)
                .last("LIMIT 1"));
    }

    // 分页查询用户信息
    default Page<UserDetailDo> selectUserDetailPageList(UserDetailQo qo) {
        // 创建分页
        Page<UserDetailDo> page = new Page<>(qo.getCurrent(), qo.getSize());

        // 构建查询条件
        LambdaQueryWrapper<UserDetailDo> wrapper = Wrappers.<UserDetailDo>lambdaQuery()
                .like(StringUtils.isNotBlank(qo.getNickname()), UserDetailDo::getNickname, qo.getNickname())
                .like(StringUtils.isNotBlank(qo.getMail()), UserDetailDo::getMail, qo.getMail())
                .like(StringUtils.isNotBlank(qo.getPhoneNumber()), UserDetailDo::getPhoneNumber, qo.getPhoneNumber())
                .like(StringUtils.isNotBlank(qo.getQqId()), UserDetailDo::getQqId, qo.getQqId())
                .like(Objects.nonNull(qo.getUserId()), UserDetailDo::getUserId, qo.getUserId()) // 模糊查询昵称，邮箱，手机号等
                .eq(Objects.nonNull(qo.getIsAdmin()), UserDetailDo::getIsAdmin, qo.getIsAdmin())
                .eq(Objects.nonNull(qo.getIdentity()), UserDetailDo::getIdentity, qo.getIdentity())
                .eq(Objects.nonNull(qo.getLevel()), UserDetailDo::getLevel, qo.getLevel()) // 比较用户身份，等级，是否为管理员
                .ge(Objects.nonNull(qo.getStartDate()), UserDetailDo::getCreateTime, qo.getStartDate()) // 大于等于 startDate
                .le(Objects.nonNull(qo.getEndDate()), UserDetailDo::getCreateTime, qo.getEndDate())  // 小于等于 endDate
                .orderByDesc(UserDetailDo::getCreateTime); // 按照创建时间倒序排序

        // 分页查询
        return selectPage(page, wrapper);
    }
}
